package com.payment.system.service.impl;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
import java.util.TreeMap;

import com.payment.common.utils.pay.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.payment.common.base.AjaxResult;
import com.payment.common.support.Convert;
import com.payment.system.domain.PlanItemNew;
import com.payment.system.domain.PlanNew;
import com.payment.system.mapper.PlanItemNewMapper;
import com.payment.system.mapper.PlanNewMapper;
import com.payment.system.service.IPlanItemNewService;

/**
 * 新计划详情 服务层实现
 * 
 * @author payment
 * @date 2019-07-02
 */
@Service
public class PlanItemNewServiceImpl implements IPlanItemNewService 
{
	@Autowired
	private PlanItemNewMapper planItemNewMapper;
	@Autowired
	private PlanNewMapper planNewMapper;
	/**
     * 查询新计划详情信息
     * 
     * @param id 新计划详情ID
     * @return 新计划详情信息
     */
    @Override
	public PlanItemNew selectPlanItemNewById(String id)
	{
	    return planItemNewMapper.selectPlanItemNewById(id);
	}
	
	/**
     * 查询新计划详情列表
     * 
     * @param planItemNew 新计划详情信息
     * @return 新计划详情集合
     */
	@Override
	public List<PlanItemNew> selectPlanItemNewList(PlanItemNew planItemNew)
	{
	    return planItemNewMapper.selectPlanItemNewList1(planItemNew);
	}
	
    /**
     * 新增新计划详情
     * 
     * @param planItemNew 新计划详情信息
     * @return 结果
     */
	@Override
	public int insertPlanItemNew(PlanItemNew planItemNew)
	{
	    return planItemNewMapper.insertPlanItemNew(planItemNew);
	}
	
	/**
     * 修改新计划详情
     * 
     * @param planItemNew 新计划详情信息
     * @return 结果
     */
	@Override
	public int updatePlanItemNew(PlanItemNew planItemNew)
	{
	    return planItemNewMapper.updatePlanItemNew(planItemNew);
	}

	/**
     * 删除新计划详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deletePlanItemNewByIds(String ids)
	{
		return planItemNewMapper.deletePlanItemNewByIds(Convert.toStrArray(ids));
	}
	
	@Override
	public AjaxResult getPlanItemList(String planId) {
		// TODO Auto-generated method stub
		List<PlanItemNew> pl = planItemNewMapper.selectPlanItemNewListByPlanId(planId);
		if (pl == null || pl.size() == 0) {
			return AjaxResult.error(1, "无计划");
		}
		BigDecimal moeny = BigDecimal.ZERO;
		int num = 0;
		int count = 0;
		for (PlanItemNew planItemNew : pl) {
			if (planItemNew.getStatus().equals(3)) {
				num++;
				if ("payment".equals(planItemNew.getMoneyType())) {
					moeny = moeny.add(planItemNew.getMoney());
				}
			}
			count++;
		}
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(2);
		PlanNew plan = planNewMapper.selectPlanNewById(planId);
		if (plan.getType().equals(2)) {
			moeny = moeny.divide(BigDecimal.valueOf(2)).setScale(2, BigDecimal.ROUND_DOWN);
		}
		String result = numberFormat.format((float) num / (float) count * 100);
		TreeMap<String, Object> map = new TreeMap<>();
		map.put("money", moeny);
		map.put("Percentage", result);
		map.put("list", pl);
		return AjaxResult.successAndData("成功", map);
	}

	@Override
	public PlanItemNew selectPlanItemNewListByOrderId(String orderId) {
		return planItemNewMapper.selectPlanItemNewListByOrderId(orderId);
	}

	@Override
	public List<PlanItemNew> selectPlanItemNewList1(PlanItemNew planItemNew) {
		List<PlanItemNew> list = planItemNewMapper.selectPlanItemNewList1(planItemNew);
		for (PlanItemNew planItemNew1 : list){
			//敏感信息脱敏
			planItemNew1.setCardNumber(EncryptUtil.desEncryptMessage(planItemNew1.getCardNumber()));
		}
		return list;
	}
}
